package com.zhou.code;

import java.lang.reflect.Field;
import java.util.Map;

public class ParamUtil {

    public static Object getParam(Object obj, String oldParamName) throws IllegalAccessException {
        int index = oldParamName.indexOf(".");
        String newParamName;
        if (index > 0) {
            newParamName = oldParamName.substring(0, index);
        } else {
            newParamName = oldParamName;
        }
        Object result = null;
        if (obj instanceof Map) {
            result = ((Map) obj).get(newParamName);
        }
        Field[] fields = obj.getClass().getFields();
        for (Field field : fields) {
            if (field.getName().equals(newParamName)) {
                result = field.get(obj);
            }
        }
        if (result == null) {
            throw new RuntimeException("不存在成员属性：" + newParamName);
        }
        if (index < 0) {
            return result;
        } else {
            return getParam(result, oldParamName.substring(index + 1));
        }
    }
}
